package com.boat.ai.dto.rsp;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @param <T>
 */
@Setter
@Getter
@ToString
@Accessors(chain = true)
public class R<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;
    /**
     * 状态码
     */
    private int code;
    /**
     * 状态信息
     */
    private String msg;
    /**
     * 数据对象
     */
    private T data;

    public static <T> R<T> ok() {
        return restResult(null, 0, "ok");
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, 0, "ok");
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, 0, msg);
    }

    public static <T> R<T> fail() {
        return restResult(null, 1, "fail");
    }

    public static <T> R<T> empty() {
        return restResult(null, 2, "empty");
    }

    public static <T> R<T> fail(String msg) {
        return restResult(null, 1, msg);
    }

    public static <T> R<T> fail(T data) {
        return restResult(data, 1, "fail");
    }

    public static <T> R<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

    public static <T> R<T> fail(T data, String msg) {
        return restResult(data, 1, msg);
    }

    public static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.code = code;
        apiResult.data = data;
        apiResult.msg = msg;
        return apiResult;
    }

}
